package io.renren.modules.sys.service.impl;

import org.springframework.stereotype.Service;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;

import io.renren.modules.sys.dao.TeacherDao;
import io.renren.modules.sys.entity.TeacherEntity;
import io.renren.modules.sys.service.TeacherService;


@Service("teacherService")
public class TeacherServiceImpl extends ServiceImpl<TeacherDao, TeacherEntity> implements TeacherService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<TeacherEntity> page = this.page(
                new Query<TeacherEntity>().getPage(params),
                new QueryWrapper<TeacherEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void saveOrUpdateByUserId(Long userId, String username, String mobile, String email) {
        TeacherEntity teacherEntity = new TeacherEntity();
        teacherEntity.setUserId(userId);
        teacherEntity.setEmail(email);
        teacherEntity.setPhoneNumber(mobile);
        teacherEntity.setRealName(username);
        TeacherEntity teacher = this.getBaseMapper().selectOne(new QueryWrapper<TeacherEntity>().
                eq("user_id", userId));
        if (teacher!=null) {
            teacherEntity.setId(teacher.getId());
            this.updateById(teacherEntity);
        } else {
            this.save(teacherEntity);
        }
    }

}